﻿using System;
using System.Data;
using System.Linq;
using System.Windows;
using CAION2.Classes;
using CAION2.Helpers;
using CAION2.Models;
using FirstFloor.ModernUI.Presentation;

namespace CAION2.ViewModels
{
    class RecipesTabViewModel : ViewModelBase
    {
        #region Vars

        private LinkCollection _links;
        private Uri _selectedLink;

        #endregion


        #region Properties

        public LinkCollection Links
        {
            get { return _links; }
            private set
            {
                if (Equals(value, _links)) return;
                _links = value;
                OnPropertyChanged("Links");
            }
        }

        public Uri SelectedLink
        {
            get { return _selectedLink; }
            private set
            {
                if (Equals(value, _selectedLink)) return;
                _selectedLink = value;
                OnPropertyChanged("SelectedLink");
            }
        }

        #endregion


        public RecipesTabViewModel()
        {
            //Mediator.Instance.Register(o => AddLink((Tuple<string, string>)o), Messages.RecipeSend);
            //Mediator.Instance.Register(o => SendRecipeToRecepture((string)o), Messages.RecipeRequire);

            Links = new LinkCollection
            {
                new Link
                {
                    DisplayName = Properties.Resources.RR_RecipesListTitle,
                    Source = new Uri("/Views/RecipesList_Page.xaml", UriKind.RelativeOrAbsolute)
                }
            };

            SelectedLink = Links[0].Source;
        }

        private void AddLink(Tuple<string, string> recipeInfo)
        {
            if (Links.All(l => l.Source.ToString() != "/CAION2;component/Views/Recepture_Page.xaml#" + recipeInfo.Item1))
            {
                if (Links.Count > ClassConfiguration.GetInt32("MaxRecipesInUse"))
                {
                    MessageBox.Show(string.Format(Properties.Resources.RR_MaxRecipesCount,
                        ClassConfiguration.GetInt32("MaxRecipesInUse")));
                    return;
                }

                DataRow row = DataModel.GetRow(recipeInfo.Item2, string.Format(" id = '{0}'", recipeInfo.Item1));

                string displayName = row["product_translate" + DataModel.EngFix].ToString();
                if (displayName.Length > 20) displayName = displayName.Substring(0, 20) + "...";

                Links.Add(new Link
                {
                    DisplayName = displayName,
                    Source =
                        new Uri("/CAION2;component/Views/Recepture_Page.xaml#" + row["id"], UriKind.RelativeOrAbsolute),
                });
            }
            
            SelectedLink =
                Links.First(
                l => l.Source.ToString() == "/CAION2;component/Views/Recepture_Page.xaml#" + recipeInfo.Item1)
                .Source;
        }

        private void SendRecipeToRecepture(string id)
        {
            Mediator.Instance.NotifyColleagues(id, Links.Last().Source.ToString());
        }

    }
}
